package com.example.employeemanagement.Controller;

import com.example.employeemanagement.dto.AttendanceListDto;
import com.example.employeemanagement.dto.AttendancePageQueryDto;
import com.example.employeemanagement.dto.PageResultDto;
import com.example.employeemanagement.entry.Attendance;
import com.example.employeemanagement.service.AttendanceService;
import com.example.employeemanagement.vo.ApiResponse;
import com.example.employeemanagement.vo.AttendanceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 考勤管理控制器
 * 提供考勤记录的CRUD操作RESTful API接口
 * 所有接口路径前缀为/api/attendances
 */
@RestController
@RequestMapping("/attendances")
public class AttendanceController {

    @Autowired
    private AttendanceService attendanceService;

    /**
     * 创建新的考勤记录
     * @param vo 考勤记录数据，通过请求体JSON传入
     * @return 创建成功的考勤记录及操作结果信息
     */
    @PostMapping
    public ResponseEntity<ApiResponse<AttendanceVo>> createAttendance(@Valid @RequestBody AttendanceVo vo) {
        try {
            AttendanceVo resultVo = attendanceService.createAttendance(vo);
            return ResponseEntity.ok(new ApiResponse<>("success", "考勤记录创建成功", resultVo));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(new ApiResponse<>("error", e.getMessage(), null));
        }
    }

    /**
     * 获取所有考勤记录列表
     * @return 考勤记录列表及操作结果信息
     */
    @GetMapping
    public ResponseEntity<ApiResponse<List<AttendanceListDto>>> getAllAttendances() {
        try {
            List<AttendanceListDto> attendances = attendanceService.getAllAttendances();
            return ResponseEntity.ok(new ApiResponse<>("success", "查询成功", attendances));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(new ApiResponse<>("error", e.getMessage(), null));
        }
    }

    /**
     * 分页查询考勤记录
     * 支持按姓名、工号、部门、考勤状态进行模糊查询
     * @param queryDto 查询参数，通过请求体JSON传入
     * @return 分页查询结果及操作结果信息
     */
    @PostMapping("/pagingQuery")
    public ResponseEntity<ApiResponse<PageResultDto<AttendanceListDto>>> pagingQuery(@RequestBody AttendancePageQueryDto queryDto) {
        try {
            PageResultDto<AttendanceListDto> result = attendanceService.pageQuery(queryDto);
            return ResponseEntity.ok(new ApiResponse<>("success", "查询成功", result));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(new ApiResponse<>("error", e.getMessage(), null));
        }
    }



    /**
     * 根据ID获取单个考勤记录
     * @param id 考勤记录ID，通过URL路径变量传入
     * @return 指定ID的考勤记录及操作结果信息
     */
    @GetMapping("/{id}")
    public ResponseEntity<ApiResponse<Attendance>> getAttendanceById(@PathVariable Integer id) {
        try {
            Attendance attendance = attendanceService.getAttendanceById(id);
            return ResponseEntity.ok(new ApiResponse<>("success", "查询成功", attendance));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(new ApiResponse<>("error", e.getMessage(), null));
        }
    }

    /**
     * 更新指定ID的考勤记录
     * @param id 要更新的考勤记录ID，通过URL路径变量传入
     * @param vo 更新后的考勤记录数据，通过请求体JSON传入
     * @return 更新后的考勤记录及操作结果信息
     */
    @PostMapping("/{id}")
    public ResponseEntity<ApiResponse<AttendanceVo>> updateAttendance(
            @PathVariable Integer id,
            @Valid @RequestBody AttendanceVo vo) {
        try {
            AttendanceVo resultVo = attendanceService.updateAttendance(id, vo);
            return ResponseEntity.ok(new ApiResponse<>("success", "考勤记录更新成功", resultVo));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(new ApiResponse<>("error", e.getMessage(), null));
        }
    }

    /**
     * 删除指定ID的考勤记录
     * @param id 要删除的考勤记录ID，通过URL路径变量传入
     * @return 操作结果信息
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<ApiResponse<Void>> deleteAttendance(@PathVariable Integer id) {
        try {
            attendanceService.deleteAttendance(id);
            return ResponseEntity.ok(new ApiResponse<>("success", "考勤记录删除成功", null));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(new ApiResponse<>("error", e.getMessage(), null));
        }
    }

    /**
     * 根据用户ID获取考勤记录列表
     * @param userId 用户ID，通过URL路径变量传入
     * @return 指定用户的考勤记录列表及操作结果信息
     */
    @GetMapping("/user/{userId}")
    public ResponseEntity<ApiResponse<List<Attendance>>> getAttendancesByUserId(@PathVariable int userId) {
        try {
            List<Attendance> attendances = attendanceService.getAttendancesAllByUserId(userId);
            return ResponseEntity.ok(new ApiResponse<>("success", "查询成功", attendances));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(new ApiResponse<>("error", e.getMessage(), null));
        }
    }
}
